package com.mongo.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;


/**
 * 扫描指定包将Mapper注入ioc
 * @author xiazhun
 * @create 2022/8/6
 */
public class CacheConfig<R> implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    @SuppressWarnings("all")
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        Environment environment = applicationContext.getEnvironment();
        String property = environment.getProperty("mongo.mapperPath");
        if (StringUtils.isBlank(property)){
            return;
        }
        //扫描注册
        ServiceInterfacesScanner scanner = new ServiceInterfacesScanner(registry);
        scanner.doScan(property);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CacheConfig.applicationContext = applicationContext;
    }
}
